#include "myVector.hpp"

using namespace std;

MyVector::MyVector()
{
    m_length = 0;
}

MyVector::MyVector(int sum, ElemType data)
{
    if(sum > maxSize)
        sum = maxSize;
    m_length = sum;
    //i=sum
    for(int i=0; i<sum; i++)
    {
        m_data[i] = data;
    }
}

MyVector:: ~MyVector()
{
    
}

int MyVector::length()
{
    return m_length;
}

bool MyVector::empty()
{
    if(m_length)
        return false;
    else
        return true;
}

void MyVector::clear()
{
    m_length = 0;
}

int MyVector::get(int index,ElemType data)
{
    if(index < 0 || index >= m_length)
        return -1;
    data = m_data[index];
    return 1;
}

int MyVector::locateElem(ElemType data)
{
    for(int i=0; i<m_length; i++)
    {
        if(data == m_data[i])
            return i;
    }
    return -1;
}

//插入到最后一个时
int MyVector::insert(int index, ElemType data)
{
    if(index < 0 || index > m_length)
        return -1;

    m_length++;
    for(int i = (m_length-1); i > index; i--)
    {
        m_data[i] = m_data[i-1]; 
    }
    m_data[index] = data;
    
    return 1;
}

int MyVector::deleteData(int index, ElemType &data)
{
    if(index < 0 || index > m_length)
        return -1;
    data = m_data[index];
    int i = index;
    while(i < m_length){
        m_data[i] = m_data[i+1];
        i++;
    }
    m_length--;
    return 1;
}

void MyVector::printElem()
{
    if(m_length == 0)
    {
        cout <<"no elems."<< endl;
        return;
    }
    cout <<"elems:[";
    for(int i=0; i< m_length; i++)
    {
        cout << m_data[i]<< ",";
    }
    cout <<"]."<< endl;
}